﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ExtensionsAPI.NetworkAPI;

namespace BleQua.Data {
	public class SettingsManager : Dictionary<string, string>, IDisposable {
		private Shared.DataEntities.ProfileContext DataContext;

		public SettingsManager(Shared.DataEntities.ProfileContext DataContext) {
			// TODO: Complete member initialization
			this.DataContext = DataContext;

			if (DataContext != null) {
				this.LoadData( );
			}
		}

		private void ReloadData( ) {
			this.Clear( );
			this.LoadData( );
		}
		private void LoadData( ) {
			foreach (var item in DataContext.Settings) {
				this.Add(item.name, item.value);
			}
		}

		public void SaveChanges( ) {
			//foreach (var item1 in this) {
			//	bool exist = false;
			//	foreach (var item2 in DataContext.Accounts) {
			//		if (item1.Identifier == item2.id)
			//			exist = true;
			//	}
			//	if (!exist)
			//		DataContext.Accounts.AddObject(new Shared.DataEntities.Account( ) { id = item1.Identifier, password = item1.Password, network = item1.PluginServer });
			//}
			//foreach (var item1 in DataContext.Accounts) {
			//	bool exist = false;
			//	foreach (var item2 in this) {
			//		if (item1.id == item2.Identifier)
			//			exist = true;
			//	}
			//	if (!exist)
			//		DataContext.Accounts.DeleteObject(item1);
			//}
			//dataContext.DetectChanges( );
			//dataContext.AcceptAllChanges( );
			DataContext.SaveChanges( );
			this.ReloadData( );
		}

		public void UndoChanges( ) {
			this.ReloadData( );
		}

		public new void RemoveAt(int index) {
			if (base.Keys.Count > index) {
				var keys = this.Keys.ToArray( );
				var key = keys[index];
				base.Remove(key);
			}
		}

		public void Dispose( ) {
			//throw new NotImplementedException( );
		}
	}
}
